package com.lanou.elh.sysmanage.system_management.service.impl;

import com.lanou.elh.sysmanage.system_management.entity.TbExpress;
import com.lanou.elh.sysmanage.system_management.mapper.ExpressMapper;
import com.lanou.elh.sysmanage.system_management.service.ExpressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ExpressServiceImpl implements ExpressService {
    @Autowired
    private ExpressMapper mapper;


    @Override
    public List<TbExpress> selectExpress() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
        List<TbExpress> TbExpressList = mapper.selectExpress();
        for (TbExpress express : TbExpressList) {
            try {
                String created = express.getCreated();
                Date create = format.parse(created);
                long createTime = create.getTime();
                String creat = String.valueOf(createTime);
                express.setCreated(creat);
                String updated = express.getUpdated();
                Date update = format.parse(updated);
                long updateTime = update.getTime();
                String updat = String.valueOf(updateTime);
                express.setUpdated(updat);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return TbExpressList;

    }

    @Override
    public boolean deleteByid(Integer id) {
        return mapper.deleteById(id);
    }

    @Override
    public boolean deleteByManyId(Integer[] id) {
        for (int i = 0; i < id.length; i++) {
            mapper.deleteByManyId(id[i]);
        }
        return true;
    }

    @Override
    public boolean addExpress(TbExpress express) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(new Date());
        express.setCreated(format);
        express.setUpdated(format);
        return mapper.addExpress(express);
    }

    @Override
    public boolean updateExpressById(TbExpress express) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(new Date());
        express.setUpdated(format);
        return mapper.updateExpressById(express);
    }
}
